package cn.itsource.smartcampus.controller;

import cn.itsource.smartcampus.domain.Student;
import cn.itsource.smartcampus.service.StudentService;
import cn.itsource.smartcampus.util.AjaxResult;
import cn.itsource.smartcampus.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生表的全部管理信息
 */
@RestController
@RequestMapping("/stu")
public class StudentController {
    @Autowired
    private StudentService service;
    //接收请求参数

    /**
     * 学生表的查询全部信息
     * @return
     */
    @GetMapping
    public AjaxResult getAll(){
        List<Student> list = service.getAll();
       return AjaxResult
               .me()
               .setSuccess(true)
               .setMessage("查询全部成功")
               .setResultObj(list);

    }

    /**
     * 查询单条学生信息
     * @param id
     * @return
     */
    @GetMapping ("/{id}")

    public AjaxResult getStuByID(@PathVariable("id") Long id){
        Student stu = service.selectByPrimaryKey(id);
        return AjaxResult
                .me()
                .setSuccess(true)
                .setMessage("查询单条成功")
                .setResultObj(stu);
    }
    /**
     * 添加学生信息
     */
    @PutMapping

    /*@RequestBody 因为我们传去的是一个json对象*/
    public AjaxResult insetStu(@RequestBody Student stu){
        int i=0;
        if(stu.getId()!=null){
          i = service.updateByPrimaryKeySelective(stu);
            return AjaxResult
                    .me()
                    .setSuccess(true)
                    .setMessage("添加成功")
                    .setResultObj(i);
        }else {
             i = service.insert(stu);
            return AjaxResult
                    .me()
                    .setSuccess(true)
                    .setMessage("添加成功")
                    .setResultObj(i);
        }
    }

    /**
     * 根据id删除学生信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        int i = service.deleteByPrimaryKey(id);
        if(i>0) {
            return AjaxResult
                    .me()
                    .setSuccess(true)
                    .setMessage("删除成功")
                    .setResultObj(i);

        }
        else {
            return AjaxResult
                    .me()
                    .setSuccess(true)
                    .setMessage("删除失败")
                    .setResultObj(i);
        }
    }
    /**
     * @Description: 学生信息导出
     * @Author: Neuronet
     * @Date: 2023/9/25 16:49
     * @Return:
     **/
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Student> list = service.getAll();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-学生信息", "学生信息表", Student.class, "students", response);
    }

    /**
     * @Description: 学生信息导入模板
     * @Author: Neuronet
     * @Date: 2023/9/25 16:49
     * @param response: 响应对象
     * @Return:
     **/
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Student> list = new ArrayList<>();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-学生信息", "学生信息表", Student.class, "学生信息导入模板", response);
    }
    /**
     * @Description: 学生信息导入
     * @Author: Neuronet
     * @Date: 2023/9/25 17:35
     * @param file: 要导入的文件
     * @Return:
     **/
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
        service.importExcel(file);
        return AjaxResult.me().setSuccess(true).setMessage("导入成功");
    }

}
